package service;

import dao.FileDao;
import entity.UpFile;
import utils.Constant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/showFile")
public class ShowFile extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();
        Object attribute = session.getAttribute(Constant.LOGIN_SESSION);
        if (attribute == null){
            resp.getWriter().write("<a href='/login'><h3 style='color:red'>请先登录</h3></a>");
            return;
        }
        try {
            List<UpFile> upFiles = FileDao.selectALl();
            req.setAttribute("upFile1", upFiles);
            req.getRequestDispatcher("/list.jsp").forward(req,resp);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
